﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.Serialization;
using System.Runtime;

using OpenQA.Selenium;
using OpenQA.Selenium.Remote;


namespace AutoTestingFramework.Core.Configurations
{
    [DataContract]

    public class Capabilties
    {
        [DataMember]
        public IList<Capability> Capabilities;
    }
    [DataContract]
    public class Capability
    {
        [DataMember]
        public string desiredCapabilityOf;
        [DataMember]
        public bool? acceptSslCertificates;
        [DataMember]
        public string browserName;
        [DataMember]
        public bool? handlesAlerts;
        [DataMember]
        public bool? hasNativeEvents;
        [DataMember]
        public bool? isJavaScriptEnabled;
        [DataMember]
        public string pageLoadStrategy;
        [DataMember]
        public string platform;
        [DataMember]
        public string proxy;
        [DataMember]
        public bool? rotatable;
        [DataMember]
        public bool? supportsFindingByCss;
        [DataMember]
        public bool? takesScreenshot;
        [DataMember]
        public string unexpectedAlertBehavior;
        [DataMember]
        public string version;

        //'handlesAlerts, hasNativeEvents, isJavaScriptEnabled, pageLoadStrategy, proxy, rotatable, supportsFindingByCss, takesScreenshot, unexpectedAlertBehavior, version' 
        public DesiredCapabilities GetDesiredCapabilities()
        {
            DesiredCapabilities desiredCapabilities = new DesiredCapabilities();

            switch (desiredCapabilityOf.ToUpper())
            {
                case "ANDROID":
                    desiredCapabilities = DesiredCapabilities.Android();
                    break;
                case "CHROME":
                    desiredCapabilities = DesiredCapabilities.Chrome();
                    break;
                case "EDGE":
                    desiredCapabilities = DesiredCapabilities.Edge();
                    break;
                case "FIREFOX":
                    desiredCapabilities = DesiredCapabilities.Firefox();
                    break;
                case "HTMLUNIT":
                    desiredCapabilities = DesiredCapabilities.HtmlUnit();
                    break;
                case "HTMLUNITWITHJAVASCRIPT":
                    desiredCapabilities = DesiredCapabilities.HtmlUnitWithJavaScript();
                    break;
                case "INTERNETEXPLORER":
                    desiredCapabilities = DesiredCapabilities.InternetExplorer();
                    break;
                case "IPAD":
                    desiredCapabilities = DesiredCapabilities.IPad();
                    break;
                case "IPHONE":
                    desiredCapabilities = DesiredCapabilities.IPhone();
                    break;
                case "OPERA":
                    desiredCapabilities = DesiredCapabilities.Opera();
                    break;
                case "PHANTOMJS":
                    desiredCapabilities = DesiredCapabilities.PhantomJS();
                    break;
                case "SAFARI":
                    desiredCapabilities = DesiredCapabilities.Safari();
                    break;
            }
            if (this.acceptSslCertificates != null)
                desiredCapabilities.SetCapability(CapabilityType.AcceptSslCertificates, this.acceptSslCertificates);
            if (this.browserName != null)
                desiredCapabilities.SetCapability(CapabilityType.BrowserName, this.browserName);
            if (this.handlesAlerts != null)
                desiredCapabilities.SetCapability(CapabilityType.HandlesAlerts, this.handlesAlerts);
            if (this.hasNativeEvents != null)
                desiredCapabilities.SetCapability(CapabilityType.HasNativeEvents, this.hasNativeEvents);
            if (this.isJavaScriptEnabled != null)
                desiredCapabilities.SetCapability(CapabilityType.IsJavaScriptEnabled, this.isJavaScriptEnabled);
            if (this.pageLoadStrategy != null)
                desiredCapabilities.SetCapability(CapabilityType.PageLoadStrategy, this.pageLoadStrategy);
            if (this.proxy != null)
                desiredCapabilities.SetCapability(CapabilityType.Proxy, this.proxy);
            if (this.rotatable != null)
                desiredCapabilities.SetCapability(CapabilityType.Rotatable, this.rotatable);
            if (this.supportsFindingByCss != null)
                desiredCapabilities.SetCapability(CapabilityType.SupportsFindingByCss, this.supportsFindingByCss);
            if (this.takesScreenshot != null)
                desiredCapabilities.SetCapability(CapabilityType.TakesScreenshot, this.takesScreenshot);
            if (this.unexpectedAlertBehavior != null)
                desiredCapabilities.SetCapability(CapabilityType.UnexpectedAlertBehavior, this.unexpectedAlertBehavior);
            if (this.version != null)
                desiredCapabilities.SetCapability(CapabilityType.Version, this.version);

            PlatformType platformType = new PlatformType();
            if (Enum.TryParse<PlatformType>(this.platform, true, out platformType))
            {
                desiredCapabilities.SetCapability(CapabilityType.Platform, new Platform(platformType));
            }
            return desiredCapabilities;

        }
    }
}